#!/bin/sh
[ -n "$CONFIG" ] && . $CONFIG
[ -z "$S" ] && { "error: S not set" ; exit 1; }
BINDIR=$(dirname $(readlink -f "$0"))
usage() {
cat << EOF
$0 pkg filename-of-template
merges pkg with template and writes the result to stdout.
EOF
}
[ -z "$2" ] && usage
pkg="$(basename "$1")"
template="$2"
[ -r "$S/pkg/$pkg" ] && [ -r "$2" ] || { echo "error could not read $S/pkg/$pkg and $2"; exit 1; }
hasbuild=false
grep -x '\[build\]' "$S/pkg/$pkg" >/dev/null 2>&1 && hasbuild=true
mirrors=$(awk -f $BINDIR/butch-printsec "$pkg" mirrors | sed 's/\n/ /g')
while read -r line ; do
if printf "%s\n" "$line" | grep -x "%BUTCH_CONFIG">/dev/null 2>&1 ; then
cat << EOF
export A=$A
export R=$R
export S=$S
export C=$C
export K=$K
export B=$B
EOF
awk -f $BINDIR/butch-printsec "$pkg" vars
cat << EOF
BUTCH_PACKAGE_NAME=$pkg
BUTCH_MIRROR_URLS="$mirrors"
EOF
elif printf "%s\n" "$line" | grep -x "%BUTCH_BUILDSCRIPT">/dev/null 2>&1 ; then
if $hasbuild ; then
	awk -f $BINDIR/butch-printsec "$pkg" build
else
	echo "exit 0"
fi
else
	printf "%s\n" "$line" | sed -e 's/%BUTCH_MIRROR_URLS/$BUTCH_MIRROR_URLS/g' \
	-e 's/%BUTCH_PACKAGE_NAME/$BUTCH_PACKAGE_NAME/g'

fi
done < "$template"
